<template>
  <div class="app-container">
    <div class="block">
      <el-row>
        <el-col :span="24">
          <el-button
            type="success"
            size="mini"
            icon="el-icon-plus"
            @click.native="add"
            >添加</el-button
          >
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            @click.native="edit"
            >修改</el-button
          >
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click.native="remove"
            >删除</el-button
          >
        </el-col>
      </el-row>
    </div>
    <el-table
      :data="list"
      v-loading="listLoading"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      @current-change="handleCurrentChange"
    >
      <el-table-column label="ID">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="名称">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="编码">
        <template slot-scope="scope">
          {{ scope.row.code }}
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="formTitle" :visible.sync="formVisible" width="70%">
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" minlength="1"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="编码" prop="code">
              <el-input v-model="form.code" minlength="1"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item>
          <el-button type="primary" @click="save">确认 </el-button>
          <el-button @click.native="formVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { remove, getList, save } from "@/api/channel";

export default {
  name: "chnnel",
  data() {
    return {
      formVisible: false,
      formTitle: "添加栏目",
      deptList: [],
      isAdd: true,
      form: {
        id: "",
        name: "",
        code: ""
      },
      total: 0,
      list: null,
      listLoading: true,
      selRow: {}
    };
  },

  computed: {
    rules() {
      return {
        cfgName: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        cfgValue: [{ required: true, message: "编码不能为空", trigger: "blur" }]
      };
    }
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      this.fetchData();
    },
    fetchData() {
      this.listLoading = true;
      getList().then(response => {
        this.list = response.data;
        this.listLoading = false;
      });
    },
    search() {
      this.fetchData();
    },
    reset() {
      this.fetchData();
    },
    handleFilter() {
      this.getList();
    },
    handleCurrentChange(currentRow, oldCurrentRow) {
      this.selRow = currentRow;
    },
    resetForm() {
      this.form = {
        id: "",
        name: "",
        code: ""
      };
    },
    add() {
      this.resetForm();
      this.formTitle = "添加栏目";
      this.formVisible = true;
      this.isAdd = true;
    },
    save() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          save({
            id: this.form.id,
            name: this.form.name,
            code: this.form.code
          }).then(response => {
            this.$message({
              message: "添加成功",
              type: "success"
            });
            this.fetchData();
            this.formVisible = false;
          });
        } else {
          return false;
        }
      });
    },
    checkSel() {
      if (this.selRow && this.selRow.id) {
        return true;
      }
      this.$message({
        message: "请选中操作项!",
        type: "warning"
      });
      return false;
    },
    edit() {
      if (this.checkSel()) {
        this.isAdd = false;
        this.form = this.selRow;
        this.formTitle = "编辑栏目";
        this.formVisible = true;
      }
    },
    remove() {
      if (this.checkSel()) {
        var id = this.selRow.id;
        this.$confirm("确定删除该条数据?", "提示", {
          cancelButtonText: "取消",
          confirmButtonText: "确定",
          type: "warning"
        })

          .then(() => {
            remove(id).then(response => {
              this.$message({
                  message:"删除成功",
                type: "success"
              });
              this.fetchData();
            });
          })
          .catch(() => {});
      }
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/common.scss";
</style>
